package com.soonfor.evaluate.presenter;

import android.app.Activity;
import android.os.Handler;

import com.estronger.shopmanage.bean.B1Login;
import com.estronger.shopmanage.utils.DataTools;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;

import com.orhanobut.hawk.Hawk;
import com.soonfor.evaluate.http.api.Request;
import com.soonfor.evaluate.http.httptools.AsyncUtils;
import com.soonfor.evaluate.activity.EvaluateRankingActivity;
import com.soonfor.evaluate.base.IEvalBasePresenter;
import com.soonfor.evaluate.bean.EvaluateRankingBean;
import com.soonfor.evaluate.tools.UserInfo;

/**
 * 作者：DC-DingYG on 2018-10-19 14:29
 * 邮箱：dingyg012655@126.com
 */
public class EvaluateRankingPresenter implements IEvalBasePresenter, AsyncUtils.AsyncCallback {

    EvaluateRankingActivity mActivity;

    public EvaluateRankingPresenter(EvaluateRankingActivity mActivity) {
        this.mActivity = mActivity;
    }
    int ranking = -1;
    public void getEvaluateRanking(final Activity mContext) {
        if (DataTools.rankingBeans != null && DataTools.rankingBeans.size() > 0) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    ranking = -1;
                    B1Login.LoginData userBean = Hawk.get(UserInfo.LOGIN_USERINFO, null);
                    if (userBean != null && !userBean.getUsercode().equals("")) {
                        for (int i = 0; i < DataTools.rankingBeans.size(); i++) {
                            if (DataTools.rankingBeans.get(i).getFusercode().equals(userBean.getUsercode())) {
                                ranking = i;
                                break;
                            }
                        }
                    }
                    new Handler(mContext.getMainLooper()).post(new Runnable() {
                        @Override
                        public void run() {
                            mActivity.showViewByData(true, DataTools.rankingBeans, ranking);
                        }
                    });
                }
            }).start();

        } else {
            Request.Evaluate.getEvaluateRanking(mContext, Request.Evaluate.GET_EVALUATERANKING, this);
        }
    }


    @Override
    public void success(int requestCode, String data) {
        if (requestCode == Request.Evaluate.GET_EVALUATERANKING) {
            B1Login.LoginData userinfo = Hawk.get(UserInfo.LOGIN_USERINFO, null);
            DataTools.rankingBeans = new ArrayList<>();
            int ranking = -1;
            try {
                JSONArray ja = new JSONArray(data);
                if (ja != null && ja.length() > 0) {
                    for (int i = 0; i < ja.length(); i++) {
                        EvaluateRankingBean rankingBean = new Gson().fromJson(ja.getJSONObject(i).toString(), EvaluateRankingBean.class);
                        if (userinfo != null && userinfo.getUsercode().equals(rankingBean.getFusercode())) {
                            ranking = i;
                        }
                        DataTools.rankingBeans.add(rankingBean);
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            mActivity.showViewByData(true, DataTools.rankingBeans, ranking);
        }
    }

    @Override
    public void fail(int requestCode, int statusCode, String data, String msg) {
        if (requestCode == Request.Evaluate.GET_EVALUATERANKING) {
            mActivity.showViewByData(false, new ArrayList<EvaluateRankingBean>(), -1);
            mActivity.showNoDataHint(msg);
        }
    }
}
